package com.wutao.panzh.index.content.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.panzh.UtilEntity.ResultVO;
import com.panzh.entity.DispalySku;
import com.panzh.entity.HotelItemImag;
import com.panzh.entity.HotelItemSpu;
import com.panzh.entity.HotelSaleAttr;
import com.panzh.myconst.CommonConst;
import com.panzh.myconst.UserContst;
import com.wutao.panzh.service.HotelItemSpuService;
import com.wutao.panzh.service.HotelSaleAttrService;
import com.wutao.panzh.service.HotelSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class IndexHotelHandler {

    @Reference
    private HotelSaleAttrService hotelSaleAttrService;

    @Reference
    private HotelItemSpuService hotelItemSpuService;

    @Reference
    private HotelSkuService hotelSkuService;

    @Autowired
    private ObjectMapper mapper;


    public List<HotelItemSpu> getHotelSpu() {
        List<HotelItemSpu> spuPage =  hotelItemSpuService.getHotelItemAll();
        List<DispalySku> skuPage = hotelSkuService.getDispalyHotelSkuPage();
        spuPage.forEach((HotelItemSpu spu)->{
            Integer spuId = spu.getId();
            List<DispalySku> skus = spu.getDispalySkus() == null ? new ArrayList<>() : spu.getDispalySkus();
            skuPage.forEach((DispalySku sku)->{
                if (spuId.compareTo(sku.getHotelItemSpuId()) != 0 ){
                    skus.add(sku);
                }
            });
            spu.setDispalySkus(skus);
        });
        return spuPage;
    }

    public List<HotelSaleAttr> getClassification() {
        return hotelSaleAttrService.getAllSaleAttr();
    }

    @RequestMapping("show_hotel_details")
    public String showHoteldetails(@RequestParam("itemId") String itemId, Model model){
        HotelItemSpu hotelItemSpu = hotelItemSpuService.getHotelItemById(itemId);
        List<HotelItemImag> imags = hotelItemSpu.getImags();
        //两者等价；
        String imagesJSON = JSON.toJSONString(imags);
//        String imagesJSON = mapper.writeValueAsString(imags);
        model.addAttribute("hotelItemSpu",hotelItemSpu);
        model.addAttribute("imagesJSON",imagesJSON);
        return "hotelDetails";
    }

}
